import { defineStore } from 'pinia';
import { ref } from 'vue';

interface UserInfo {
    userId: string;
    username: string;
    nickname: string;
    role: string; // 确保字段名与传入数据一致
    email: string;
}

export const useAuthStore = defineStore('auth', () => {
    const token = ref<string>('');
    const userInfo = ref<UserInfo | null>(null);

    const login = (payload: { token: string; user: UserInfo }) => {
        token.value = payload.token;
        userInfo.value = payload.user; // 确保 role 字段被正确保存
    };

    const logout = () => {
        token.value = '';
        userInfo.value = null;
    };

    return {
        token,
        userInfo,
        login,
        logout,
    };
});